package com.renrenyou.duanshipin.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.renrenyou.duanshipin.R;
import com.renrenyou.duanshipin.databinding.LayoutTitleBarBinding;

public class TitleBarView extends FrameLayout implements View.OnClickListener {
    private LayoutTitleBarBinding mBinding;

    public TitleBarView(@NonNull Context context) {
        this(context, null);
    }

    public TitleBarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBarView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mBinding = LayoutTitleBarBinding.inflate(LayoutInflater.from(context), this, true);
        mBinding.ivTitleBack.setOnClickListener(this);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
        String title = array.getString(R.styleable.TitleBarView_tbv_title_name);
        String subTitle = array.getString(R.styleable.TitleBarView_tbv_title_subname);
        mBinding.tvTitleName.setText(title);
        mBinding.tvTitleSubname.setText(subTitle);
        array.recycle();
    }

    public void setTitle(CharSequence title) {
        mBinding.tvTitleName.setText(title);
    }

    public View getSubTitle() {
        return mBinding.tvTitleSubname;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_title_back) {
            if (getContext() instanceof Activity) {
                ((Activity) getContext()).finish();
            }
        }
    }
}
